<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch">
      <el-form-item label="类别" prop="categoryId">
        <el-select v-model="queryParams.categoryId" placeholder="类别" clearable style="width: 200px">
          <el-option
              v-for="dict in categoryList"
              :key="dict.id"
              :label="dict.categoryName"
              :value="dict.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="名称" prop="productName">
        <el-input
            v-model="queryParams.productName"
            placeholder="请输名称"
            clearable
            style="width: 200px"
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="dataList" @selection-change="handleSelectionChange">
      <el-table-column label="类别" align="center" prop="categoryName" />
      <el-table-column label="配件编号" align="center" prop="serialNumber" />
      <el-table-column label="配件名称" align="center" prop="productName" />
<!--      <el-table-column label="售价" align="center" prop="saleAmount" />-->
      <el-table-column label="售价" align="center">
        <template #default="scope">
          <span style="margin-left: 10px">¥{{ scope.row.saleAmount }}</span>
        </template>
      </el-table-column>
      <el-table-column label="单位" align="center" prop="unit" />
      <el-table-column label="操作" width="180" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" >修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
        v-show="total > 0"
        :total="total"
        v-model:page="queryParams.current"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
    />

    <!-- 添加或修改岗位对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="formRef" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="类别" prop="categoryId">
          <el-select v-model="form.categoryId" placeholder="类别" clearable style="width: 200px">
            <el-option
                v-for="dict in categoryList"
                :key="dict.id"
                :label="dict.categoryName"
                :value="dict.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="配件编号" prop="serialNumber">
          <el-input v-model="form.serialNumber" placeholder="请输入配件编号" />
        </el-form-item>
        <el-form-item label="配件名称" prop="productName">
          <el-input v-model="form.productName" placeholder="请输入配件名称" />
        </el-form-item>
        <el-form-item label="配件售价" prop="saleAmount">
          <el-input v-model="form.saleAmount" placeholder="请输入配件售价" />
        </el-form-item>
        <el-form-item label="配件单位" prop="unit">
          <el-select v-model="form.unit" placeholder="配件单位" clearable style="width: 200px">
            <el-option
                v-for="dict in unitList"
                :key="dict.id"
                :label="dict.unit"
                :value="dict.id"
            />
          </el-select>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Post">
import { list,update,deleteById,add,queryCategoryList,queryUnitList} from "@/api/repair/serve/accessoryList";

const { proxy } = getCurrentInstance();

const unitList = ref([]);
const categoryList = ref([]);
const dataList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    current: 1,
    pageSize: 10,
    categoryId: undefined,
    productName: undefined,
    type: 1,
  },
  rules: {
    categoryId: [{ required: true, message: "配件类别不能为空", trigger: "blur" }],
    serialNumber: [{ required: true, message: "配件编号不能为空", trigger: "blur" }],
    productName: [{ required: true, message: "配件名称不能为空", trigger: "blur" }],
    saleAmount: [{ required: true, message: "配件售价不能为空", trigger: "blur" }],
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询单位列表 */
function getUnitList() {
  queryUnitList()
      .then((response) => {
        console.log(response)
        unitList.value = response.data
      })
}

/** 查询类别列表 */
function getCategoryList() {
  queryCategoryList()
      .then((response) => {
        console.log(response)
        categoryList.value = response.data
      })
}

/** 查询列表 */
function getList() {
  loading.value = true
  list(queryParams.value)
      .then((response) => {
        console.log(response)
        dataList.value = response.data.data
        total.value = response.data.total
        loading.value = false
      })
      .catch((err) => {
        loading.value = false
      })
}

/** 取消按钮 */
function cancel() {
  open.value = false;
  reset();
}

/** 表单重置 */
function reset() {
  form.value = {
    categoryId: undefined,
    serialNumber: undefined,
    productName: undefined,
    saleAmount: undefined,
    unit: undefined,
  };
  proxy.resetForm("formRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.current = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}
/** 新增按钮操作 */
function handleAdd() {
  reset()
  open.value = true
  title.value = '添加配件列表信息'
}
/** 修改按钮操作 */
function handleUpdate(row) {
  console.log(row)
  form.value = { ...row }
  open.value = true
  title.value = '修改配件列表信息'
}

/** 提交按钮:等待新增和修改请求接口 */
function submitForm() {
  proxy.$refs['formRef'].validate((valid) => {
    if (valid) {
      if (form.value.id != null) {
        update(form.value).then((response) => {
          proxy.$modal.msgSuccess('修改配件列表成功')
          open.value = false
          getList()
        })
      }else {
        add(form.value).then((response) => {
          proxy.$modal.msgSuccess('新增配件列表成功')
          open.value = false
          getList()
        })
      }
    }
  })
}
/** 删除按钮操作 */
function handleDelete(row) {
  console.log(row)
  proxy.$modal
      .confirm('是否确认删除 "' + row.precheckName + '" 配件列表？')
      .then(function () {
        return deleteById(row.id)
      })
      .then(() => {
        getList()
        proxy.$modal.msgSuccess('删除成功')
      })
      .catch(() => { })
}
getList();
getCategoryList();
getUnitList();
</script>
